* ****************************************************************************
* PREPARE MICRO DATA FOR ESTIMATION	
* ****************************************************************************

set more off
local origdata="/home/group/main/894ic/imprese/iai/iai20224/storico/dati"
use `origdata'/storico_iai_completo, clear 

gen dp=yq(anno,trime)
gen time=dp
format dp %tq

keep if dp>=yq(2012,3)  /* before 2012:3 all firms presented with signal */
keep if dp<=yq(2022,4)  /* sample underlying paper results is 2012:3 - 2022:4 */

*** define number of trims as global variable to be used in loops and matrix set ups
global firsttrim=yq(2012,3)
global lasttrim=yq(2022,4)
global ntrims = $lasttrim - $firsttrim + 1


* ********* NOTE ********* 
* Inflation expectations of firms exposed to signals or not exposed are stored in different variables
* Example: 
*  it12 !=. for all that are exposed to current inflation signal, and =. otherwise
*  it12e!=. for all that are exposed to ECB target signal, and =. otherwise
*  it12n!=. for all that are not exposed to any signal, and =. otherwise


* annnual inflation expectations available at different horizons
* it6  - annual expected inflation 6 months ahead
* it12 - annual expected inflation 12 months ahead
* it24 - annual expected inflation 24 months ahead
* it48 - annual expected inflation between 3 and 5 years ahead (average) 
* ************************

* drop treated with ECB target (introduced since 2017:2)
drop if it12e!=.


* Firm ID
egen ID=group(user)
tsset ID dp


gen trim=10*anno+trime


* *** gen Anchor=(formdom==2|formdom==.) if trim<=20173
* *** replace Anchor=1 if trim>=20174 & (it12!=.|it12e!=.)

gen treatment_old=(formdom==2)

* INFLATION EXPECTATIONS
* define single variable containing infl exp (for each horizon) irrespective of info treatment received.
* 6-months ahead 
gen it6all=it6
replace it6all=it6n if it6n!=.

* 12-months ahead
gen it12all=it12
replace it12all=it12n if it12n!=.

* 24 months ahead
gen it24all=it24
replace it24all=it24n if it24n!=.

* 3-5 years ahead
gen it48all=it48
replace it48all=it48n if it48n!=.


* input  infl data presetned to firms in subsequent surveys
quietly{
	
gen infl=.
*** 2012
replace infl=3.4 if time==208
replace infl=3.7 if time==209
replace infl=3.6 if time==210
replace infl=2.8 if time==211

*** 2013
replace infl=2.4 if time==212
replace infl=1.3 if time==213
replace infl=1.2 if time==214
replace infl=0.8 if time==215

*** 2014
replace infl=0.6 if time==216
replace infl=0.5 if time==217
replace infl=0   if time==218
replace infl=0.2 if time==219

*** 2015
replace infl=-0.5 if time==220
replace infl=-0.1 if time==221
*	replace infl=-0.1 if time==222  /* WRONG DATA IMPUTED IN CGR */
replace infl=0.5 if time==222
*	replace infl=-0.1 if time==223 /* WRONG DATA IMPUTED IN CGR*/
replace infl = 0.3 if time==223

*** 2016
*	replace infl=-0.1 if time==224 /* WRONG DATA IMPUTED IN CGR */
replace infl=0.4 if time==224
replace infl=-0.4 if time==225
replace infl=-0.2 if time==226
replace infl=-0.1 if time==227

*** 2017
replace infl=1.0 if time==228
*	replace infl=1.9 if time==229 /* WRONG DATA IMPUTED IN CGR*/ 
replace infl=2.0 if time==229
replace infl=1.2 if time==230
replace infl=1.1 if time==231

*** 2018
replace infl=1.2 if time==232
replace infl=0.6 if time==233
replace infl=1.9 if time==234
replace infl=1.7 if time==235

*** 2019
replace infl=0.9 if time==236
replace infl=1.1 if time==237
replace infl=0.3 if time==238
replace infl=0.2 if time==239

*** 2020
replace infl=0.4 if time==240
replace infl=0.1 if time==241
replace infl=0.8 if time==242
replace infl=-0.6 if time==243

*** 2021
replace infl=0.7 if time==244
replace infl=1.0 if time==245
replace infl=1.0 if time==246
replace infl=3.2 if time==247

*** 2022
replace infl=5.1 if time==248
replace infl=6.3 if time==249
replace infl=8.4 if time==250
replace infl=12.6 if time==251


* store infl data in matrix
mat def INFL=J(42,2,0)
forvalues j=1/42{
	local q= 209+$_j
	mat INFL[$_j,1] = $_q
	qui sum infl if time==$_q
	mat INFL[$_j,2] = r(mean)
}


* define matrix with percentiles of quarter-specific distribution of expected inflation of uninformed firms where inflation signal falls - for all hzs

mat def REFINFL_NOW=J(42,5,0)
mat def REFINFL_SIW=J(42,5,0)
quietly{
local row=1
forvalues q=210/251{
	mat REFINFL_NOW[$_row,1]=$_q
	mat REFINFL_SIW[$_row,1]=$_q
	local col=2
	foreach z in 6 12 24 48{
		count if treatm==0 & it`z'all!=. & time==$_q
		local tot=r(N)
		count if treatm==0 & it`z'all<=INFL[$_row,2] & time==$_q
		local below=r(N)
		mat REFINFL_NOW[$_row,$_col]=100*$_below/$_tot
		
		sum peso if treatm==0 & it`z'all!=. & time==$_q
		local tot=r(sum)
		sum peso if treatm==0 & it`z'all<=INFL[$_row,2] & time==$_q
		local below=r(sum)
		mat REFINFL_SIW[$_row,$_col]=100*$_below/$_tot
		
		local col=$_col + 1
	}
	local row=$_row+1
}
}


}


gen Treat=treatm*infl


**** TRIMMING - WEIGHTED
quietly{
gen q=.
forvalues j=$firsttrim/$lasttrim{
forvalues t=0/1{
xtile tmp=it12all [aw=peso] if dp==$_j & treatm==$_t, nq(100)
replace q=tmp  if dp==$_j & treatm==$_t
drop tmp
}
}
}

gen trims=(q<=2|q>=99)  /* quarter and treatmetn specific bottom-top 2percent of (weighted) sample */

gen dim= .
replace dim = 1 if inrange(add, 50, 99)
replace dim = 2 if inrange(add, 100, 149)
replace dim = 3 if inrange(add, 150, 199)
replace dim = 4 if inrange(add, 200, 249)
replace dim = 5 if inrange(add, 250, 299)
replace dim = 6 if inrange(add, 300, 499)
replace dim = 7 if inrange(add, 500, 999)
replace dim = 8 if inrange(add, 1000, 1499)
replace dim = 9 if add>=1500


tsset ID dp

* ============================
*** OTHER CONTEMPORANEOUS OUTCOMES

* Defined as in CGR
gen     PMIG=.
replace PMIG=0            if promig==1
replace PMIG=(25-1)/2     if promig==2
replace PMIG=25+(50-26)/2 if promig==3
replace PMIG=50+(75-51)/2 if promig==4
replace PMIG=75+(99-76)/2 if promig==5
replace PMIG=100          if promig==6

gen     SIMP=sitimp5-3
gen     SGEN=sitgen-2


* FORWARD OUTCOMES - subsequent (log) labor demand and own price changes

cap drop ladd
gen ladd=log(add)
gen F1LAD=f.ladd    /*next quarter lab demand */
gen F2LAD=f2.ladd   /* 2 qtrs ahead lab demand */
gen F3LAD=f3.ladd   /* 3 qtrs ahead lab demand */
gen F4LAD=f4.ladd   /* 4 qtrs ahead lab demand */

* *** NOTE 
* I also use one year ahead expected price change collected at time of interview, dprez
gen F1DPZ = f1.dpre  /* OBSERVED yearly price change one  quarte ahead */
gen F2DPZ = f2.dpre  /* OBSERVED yearly price change two  quartes ahead */
gen F4DPZ = f4.dpre  /* OBSERVED yearly price change four quartes ahead */



keep dp trim trimestre peso ID trims infl treat* Treat add ladd dpre dim setton5 area F* dprez it6all it12all it24all it48all PMIG SIMP SGEN
lab var dp "Date, quarterly (2012:3,..., 2022:4)"
lab var trim "Date, numerical (20123,...,20224)"
lab var trimestre "Quarter (1,...,4)"
lab var peso "Sampling weight"
lab var ID "Firm anonymised ID"
lab var trims "=1 if in top/bottom 2%"
lab var  infl "Inflation rate presented in wave DP"
lab var treatment "Info assignment status: 1= presented with inflation data, 0 otherwise"
lab var Treat "CGR treatment variable, Treat = infl*treatment"
lab var add "Number of employees"
lab var ladd "Number of employees (log)"
lab var dpre "Own price annual % change in past 12 months"
lab var dim "Size class"
lab var setton5 "Industry"
lab var area "Geo"
lab var F1LAD  "Log Lab demand 1 qtr after assignment"
lab var F2LAD  "Log Lab demand 2 qtr after assignment"
lab var F3LAD  "Log Lab demand 3 qtr after assignment"
lab var F4LAD  "Log Lab demand 4 qtr after assignment"
lab var F1DPZ  "Own price annual % change in past 12 months observed 1 qtr after assigment"
lab var F2DPZ  "Own price annual % change in past 12 months observed 2 qtr after assigment"
lab var F4DPZ  "Own price annual % change in past 12 months observed 4 qtr after assigment"
lab var dprez  "Own price annual % change expected over next 12 months"
lab var it6all  "Annual consumer price inflation expected over next 6 months"
lab var it12all "Annual consumer price inflation expected over next 12 months"
lab var it24all "Annual consumer price inflation expected over next 24 months"
lab var it48all "Annual consumer price inflation expected on average between next 3 and 5 years"
lab var PMIG "Probability that outlook improves over next 3 months"
lab var SIMP "Firm outlook over next 3 months"
lab var SGEN  "Overall macro outllok over past"


save WorkingData, replace
